/**
 * 
 */
package com.yst.metho.web.application.projectversion.views;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.NamingException;

import nextapp.echo2.app.Component;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.SelectField;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;

import com.yst.metho.web.application.components.listingview.ToolbarAction;
import com.yst.metho.web.application.interfaces.IListingViewModel;
import com.yst.metho.web.application.projectversion.actions.wbs.AffectActivityAction;
import com.yst.metho.web.application.projectversion.actions.wbs.CreateActivityAction;
import com.yst.metho.web.application.projectversion.actions.wbs.EditActivityAction;
import com.yst.metho.web.application.projectversion.actions.wbs.ReportActivityAction;
import com.yst.metho.web.application.projectversion.renderer.ProjectLotListRenderer;
import com.yst.metho.web.application.projectversion.renderer.ProjectVersionListRenderer;
import com.yst.metho.web.application.projectversion.renderer.ProjectWBSTableRenderer;
import com.yst.metho.web.application.utils.AbstractSuiviComparator;
import com.yst.metho.web.application.utils.AbstractSuiviTableModel;
import com.yst.metho.web.application.utils.ExceptionMgt;
import com.yst.metho.web.application.utils.GenericModel;
import com.yst.metho.web.application.utils.Locator;
import com.yst.suivi.data.activities.Activity;
import com.yst.suivi.data.project.Project;
import com.yst.suivi.data.project.versions.ProjectVersion;
import com.yst.suivi.data.project.versions.lot.Lot;

/**
 * @author perso
 * 
 */
public class ProjectWBSListModel extends AbstractSuiviTableModel implements IListingViewModel, ActionListener
{
	public static final String PROJECT_VERSION_SELECTION = "ProjectVersionSelection";

	public static final String PROJECT_LOT_SELECTION = "ProjectLotSelection";

	private class ProjectWBSComparator extends AbstractSuiviComparator
	{

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
		 */
		public int compare(Object o1, Object o2)
		{
			Activity u1 = (Activity) o1;
			Activity u2 = (Activity) o2;
			int comparaison = 0;
			switch (getColumn())
			{
			case ProjectWBSTableRenderer.NAME_COLUMN:
				comparaison = u1.getNom().compareToIgnoreCase(u2.getNom());
				break;
			case ProjectWBSTableRenderer.DURATION_COLUMN:
				if (u1.getDuree().floatValue() > u2.getDuree().floatValue())
				{
					comparaison = 1;
				}
				else
				{
					comparaison = 2;
				}
			case ProjectWBSTableRenderer.FOLDER_COLUMN:
				comparaison = u1.getFolder().getNom().compareToIgnoreCase(u2.getFolder().getNom());
				break;
			case ProjectWBSTableRenderer.USER_COLUMN:
				comparaison = 0;
				break;
			case ProjectWBSTableRenderer.STATUS_COLUMN:
				if (u1.getStatus() > u2.getStatus())
				{
					comparaison = 1;
				}
				else
				{
					comparaison = 2;
				}
				break;
			case ProjectWBSTableRenderer.LOT_COLUMN:
				if (null != u1.getLot() && null != u2.getLot())
				{
					comparaison = u1.getLot().getNom().compareToIgnoreCase(u2.getLot().getNom());
				}
				else
				{
					comparaison = 0;
				}
				break;
			default:
				break;
			}
			if (!isAscending())
			{
				comparaison = -1 * comparaison;
			}
			return comparaison;
		}

	}

	private String[] mColumnsName =
	{ "Nom", "Duree", "Classification", "Collaborateur", "Etat", "Lot" };

	// private Collection mActivitiesList;

	private ProjectVersion mSelectedPrjVersion;

	private Lot mSelectedLot;

	private SelectField mVersionList;

	private SelectField mLotList;

	private ProjectVersionListModel mPrjVersionListModel;

	private Project mPrj;

	public ProjectWBSListModel(ProjectVersionListModel prjVersionListModel, Project prj) throws NamingException, FinderException, CreateException
	{
		super();
		mPrjVersionListModel = prjVersionListModel;
		mPrj = prj;
		setComparator(new ProjectWBSComparator());
		refresh();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.metho.web.application.interfaces.IListingViewModel#getToolbarAction()
	 */
	public List getToolbarAction()
	{
		ArrayList actions = new ArrayList();
		actions.add(new CreateActivityAction(this, mVersionList, mLotList, mPrj));
		ToolbarAction action;
		action = new EditActivityAction(this, mVersionList, mPrj);
		mLotList.addActionListener(action);
		mVersionList.addActionListener(action);
		actions.add(action);
		action = new AffectActivityAction(this, mPrj);
		mLotList.addActionListener(action);
		mVersionList.addActionListener(action);
		actions.add(action);
		action = new ReportActivityAction(mVersionList, mPrj);
		actions.add(action);
		return actions;
	}

	public Component getVersionChoiceBox()
	{
		mVersionList = new SelectField();
		mVersionList.setWidth(new Extent(80, Extent.PX));
		mVersionList.setCellRenderer(new ProjectVersionListRenderer());
		mVersionList.setModel(mPrjVersionListModel);
		mVersionList.addActionListener(this);
		mVersionList.setActionCommand(PROJECT_VERSION_SELECTION);
		return mVersionList;
	}

	public Component getLotChoiceBox()
	{
		mLotList = new SelectField();
		mLotList.setWidth(new Extent(80, Extent.PX));
		mLotList.setCellRenderer(new ProjectLotListRenderer());
		mLotList.addActionListener(this);
		mLotList.setActionCommand(PROJECT_LOT_SELECTION);
		return mLotList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.metho.web.application.interfaces.IListingViewModel#refresh()
	 */
	public void refresh()
	{
		try
		{
			if (null != mLotList)
			{
				if (!mLotList.getSelectionModel().isSelectionEmpty())
				{
					mList = Locator.getActivityView().findByLot(mSelectedLot);
				}
				else
				{
					mList = new ArrayList();
				}
			}
			else
			{
				mList = new ArrayList();
			}
			fireTableDataChanged();
		}
		catch (NamingException e)
		{
			throw new RuntimeException(e);
		}
		catch (FinderException e)
		{
			throw new RuntimeException(e);
		}
		catch (CreateException e)
		{
			throw new RuntimeException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nextapp.echo2.app.table.AbstractTableModel#getColumnName(int)
	 */
	public String getColumnName(int arg0)
	{
		return mColumnsName[arg0];
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nextapp.echo2.app.table.TableModel#getColumnCount()
	 */
	public int getColumnCount()
	{
		return mColumnsName.length;
	}

	public void actionPerformed(ActionEvent e)
	{
		if (PROJECT_VERSION_SELECTION.equals(e.getActionCommand()))
		{
			if (-1 != mVersionList.getSelectedIndex())
			{
				int selectedIndex = mVersionList.getSelectionModel().getMinSelectedIndex();
				mSelectedPrjVersion = (ProjectVersion) mPrjVersionListModel.get(selectedIndex);
				try
				{
					GenericModel model = new GenericModel(Locator.getLotView().findLot(mSelectedPrjVersion.getId()));
					razLot(model);
				}
				catch (Throwable e1)
				{
					ExceptionMgt.manage(e1);
				}
			}
			else
			{
				razLot(new GenericModel(new ArrayList()));
			}
			refresh();
		}
		else if (PROJECT_LOT_SELECTION.equals(e.getActionCommand()))
		{
			if (-1 != mLotList.getSelectedIndex())
			{
				int selectedIndex = mLotList.getSelectionModel().getMinSelectedIndex();
				mSelectedLot = (Lot) mLotList.getModel().get(selectedIndex);
				refresh();
			}
		}
	}

	/**
	 * @param model
	 */
	private void razLot(GenericModel model)
	{
		mLotList.setModel(model);
		mLotList.getSelectionModel().clearSelection();
		mSelectedLot = null;
	}

	/**
	 * @return the prj
	 */
	public Project getPrj()
	{
		return mPrj;
	}
}
